package com.atguigu.dga.governance.assessor.calc;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.dga.governance.assessor.Assessor;
import com.atguigu.dga.governance.bean.AssessParam;
import com.atguigu.dga.governance.bean.GovernanceAssessDetail;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @Description: 是否长期无访问
 * 一张表{days}天内没有访问 则给0分 ， 其余给10
 * @Author: lay
 * @Date: 2024/6/28 15:15
 */
@Component("TABLE_NO_ACCESS")
public class TableNoAccessAssessor extends Assessor {
    @Override
    public void checkProblem(GovernanceAssessDetail governanceAssessDetail, AssessParam assessParam) throws Exception {
        
        //获取参数
        String paramsJson = assessParam.getGovernanceMetric().getMetricParamsJson();
        JSONObject jsonObject = JSONObject.parseObject(paramsJson);
        Integer days = jsonObject.getInteger("days");

        //最后访问时间
        Date tableLastAccessTime = assessParam.getTableMetaInfo().getTableLastAccessTime();
        Date currentDate = DateUtils.truncate(tableLastAccessTime, Calendar.DATE);
        //考评日期
        Date assessDate = DateUtils.parseDate(assessParam.getAssessDate(), "yyyy-MM-dd");
        long dateDiff = Math.abs(assessDate.getTime() - currentDate.getTime());
        long dateDiffDays = TimeUnit.DAYS.convert(dateDiff, TimeUnit.MILLISECONDS);
//        long dateDiffDays = 100L;

        if (dateDiffDays > days){
            governanceAssessDetail.setAssessScore(BigDecimal.ZERO);
            governanceAssessDetail.setAssessProblem("长期无访问");
        }
        governanceAssessDetail.setAssessComment("实际无访问" + dateDiffDays + "天");
    }
}
